package com.kmproject.haifu.mvp.ui.bis.recharge;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.jess.arms.widget.CustomPopupWindow;
import com.kmproject.haifu.R;
import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.mvp.model.entity.beans.ExchangeRateBean;
import com.kmproject.haifu.mvp.model.entity.beans.HousePayWayBean;
import com.kmproject.haifu.mvp.ui.adapter.MemberCenterPayWayAdapter;

import java.util.List;

public class PopupWindowRecharge {
    private static CustomPopupWindow customPopupWindow;
    private LinearLayout popuwindowPayClose;
    private TextView popuwindowPayJrhl;
    private TextView popuwindowPayDqhlyh;
    private TextView popuwindowPayDqhl;
    private TextView popuwindow_pay_money;
    private TextView popuwindowPaySure;
    private RecyclerView popuwindow_pay_RecyclerView;
    private MemberCenterPayWayAdapter memberCenterPayWayAdapter;

    private PopupWindowRecharge() {
    }

    private static class SingleHolder {
        @SuppressLint("StaticFieldLeak")
        private static PopupWindowRecharge INSTANCE = new PopupWindowRecharge();
    }

    public static PopupWindowRecharge getInstance() {
        return SingleHolder.INSTANCE;
    }


    public CustomPopupWindow getView(Activity activity, View mParentView, List<HousePayWayBean> housePayWayBeans, ExchangeRateBean exchangeRateBean,
                                     MemberCenterPayWayAdapter.ItemClickBack clickBack, View.OnClickListener confirmPayListener) {
        if (customPopupWindow == null) {
            customPopupWindow = new CustomPopupWindow.Builder().parentView(mParentView).contentView(CustomPopupWindow.inflateView(activity, R.layout.popuwindow_pay)).backgroundDrawable(activity.getResources().getDrawable(R.drawable.button_white_bg))
                    .animationStyle(R.style.mypopwindow_anim_style).customListener(contentView -> {
                        popuwindowPayClose = (LinearLayout) contentView.findViewById(R.id.popuwindow_pay_close);
                        popuwindowPayJrhl = (TextView) contentView.findViewById(R.id.popuwindow_pay_jrhl);
                        popuwindowPayDqhlyh = (TextView) contentView.findViewById(R.id.popuwindow_pay_dqhlyh);
                        popuwindowPayDqhl = (TextView) contentView.findViewById(R.id.popuwindow_pay_dqhl);
                        popuwindow_pay_money = (TextView) contentView.findViewById(R.id.popuwindow_pay_money);

                        popuwindow_pay_RecyclerView = (RecyclerView) contentView.findViewById(R.id.popuwindow_pay_RecyclerView);
                        popuwindow_pay_RecyclerView.setLayoutManager(new LinearLayoutManager(activity));
                        memberCenterPayWayAdapter = new MemberCenterPayWayAdapter(housePayWayBeans, clickBack);
                        popuwindow_pay_RecyclerView.setAdapter(memberCenterPayWayAdapter);

                        popuwindowPaySure = (TextView) contentView.findViewById(R.id.popuwindow_pay_sure);

                        popuwindowPaySure.setOnClickListener(confirmPayListener);
                        popuwindowPayClose.setOnClickListener(v -> customPopupWindow.dismiss());
                    }).isWrap(true).isHaveBackGround(true).isShowBottom(true).build();
        } else {
            memberCenterPayWayAdapter.setItemClickBack(clickBack);
            memberCenterPayWayAdapter.notifyDataSetChanged();
            popuwindowPaySure.setOnClickListener(confirmPayListener);
        }
        popuwindowPayJrhl.setText("1:" + exchangeRateBean.getCnyExchangeRate());
        if (exchangeRateBean.getRebate() != null && !TextUtils.isEmpty(exchangeRateBean.getRebate()) && Double.parseDouble(exchangeRateBean.getRebate()) > 0 && Double.parseDouble(exchangeRateBean.getRebate()) < 10) {
            popuwindowPayDqhlyh.setText("当前汇率(已优惠" + exchangeRateBean.getRebate() + "折)");
        } else {
            //没有优惠
            popuwindowPayDqhlyh.setVisibility(View.GONE);
            popuwindowPayDqhl.setVisibility(View.GONE);
        }

        popuwindowPayDqhl.setText("1:" + exchangeRateBean.getCnyDiscountExchangeRate());
        popuwindow_pay_money.setText("￥ " + AppUtil.getShowMenoy(exchangeRateBean.getDiscountAmount()));

        popuwindowPaySure.setText("确认支付 ￥ " + exchangeRateBean.getDiscountAmount());
        return customPopupWindow;
    }
}
